package cn.itcast.mq;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

/**
 * ClassName:SpringRabbitListener
 * Package:cn.itcast.mq
 * Description:
 * Date:2021/11/21 3:44
 * Author:liu.min
 */
@Component
public class SpringRabbitListener {
    @RabbitListener(queues = "object.queue")
    public void listenSimpleQueueMessageJson(Map<String,Object> msg) throws InterruptedException {
        System.out.println("jsonConverter 消费者接收到消息 ：【" + msg + "】");
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessageFirst(String msg) throws InterruptedException {
        System.out.println("spring 消费者接收到消息 ：【" + msg + "】");
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
        System.out.println("spring 消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(25);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage2(String msg) throws InterruptedException {
        System.err.println("spring 消费者2接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(100);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueueMessage(String msg) throws InterruptedException {
        System.out.println("Fanout 消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(25);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueueMessage2(String msg) throws InterruptedException {
        System.err.println("Fanout 消费者2接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(100);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {
                    "red",
                    "blue"
            }
    ))
    public void listenDireatQueueMessage(String msg) throws InterruptedException {
        System.err.println("direct 消费者1接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {
                    "red",
                    "yellow"
            }
    ))
    public void listenDireatQueueMessage2(String msg) throws InterruptedException {
        System.err.println("direct 消费者2接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "中国.#"
    ))
    public void listenTopicQueueMessage(String msg) throws InterruptedException {
        System.err.println("topic 消费者1接收到消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = "#.MP4"
    ))
    public void listenTopicQueueMessage2(String msg) throws InterruptedException {
        System.err.println("topic 消费者2接收到消息：【" + msg + "】");
    }
}
